<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload-ui.css" />
<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload.css" />
<link rel="stylesheet" href="__PUBLIC__/css/select2/select2.min.css" />
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" id="轮播" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"></span></button>
            <h4 class="modal-title" id="exampleModalLabel">添加轮播</h4>
        </div>
        <form class="form-horizontal" style="padding:4%;height:500px;overflow-y:auto;" data-toggle="validator" id="addAdvForm">
            <div class="form-group">
                <label for="recipient-name" class="control-label col-sm-3"><span style="color:red;">*</span>轮播名称:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="name"  placeholder="请输入名称"  value="" required>
                    <!--<div class="help-block with-errors"></div>-->
                </div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label col-sm-3">按钮文本:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="button_text" value="{$orgInfo.button_text}" placeholder="请输入按钮文本">
                </div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label col-sm-3">内容描述:</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" name="introduce" value="" placeholder="请输入内容描述" >
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label col-sm-3"><span style="color:red;">*</span>广告位:</label>
                <div class="col-sm-4">
                    <select name="prid" class="form-control">
                        <option value="">--请选择--</option>
                        <volist name="advPlaces" id="item">
                            <option value="{$item.id}">{$item.name}</option>
                        </volist>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label col-sm-3"><span style="color:red;">*</span>轮播动作对象:</label>
                <div class="col-sm-4">
                    <select name="adv_object" class="form-control">
                        <option value="">--请选择--</option>
                        <volist name="advObj" id="ao">
                            <option value="{$key}">{$ao}</option>
                        </volist>
                       
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label col-sm-3"><span style="color:red;">*</span>轮播动作对象ID:</label>
                <div class="col-sm-9">
                    <select name="adv_object_id" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label col-sm-3">优先级:</label>          
                <div class="col-sm-2">
                        <input type="text" class="form-control " name="position" value="" >
                    </div>
            </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label col-sm-3">广告图片:</label>
                    <div class="col-sm-4">
                        <img id="advImg" src="/louyu/Public/images/avatar.png" style="width:150px;">
                        <input id="fileupload" type="file" name="files[]" multiple="" style="display: none;">
                    </div>
                </div>
            <input type="hidden" name="resource" id="resource" value="/images/avatar.png" />
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="advAdd" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/vendor/jquery.ui.widget.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-ui.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-process.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/select2/select2.min.js"></script>
<script>
    
     $("select[name='adv_object_id']").select2({
            placeholder: "选择用户组",
            width: "100%"
        });
        
    $(function () {
        $('#addAdvForm').validator();
    })
    
    $("select[name='adv_object']").change(function(){
        var type  = $(this).val();
        $.ajax({
            url:"{:U('Admin/Content/getAdvObjectList')}",
            type:"post",
            dataType:"json",
            data:{type:type},
            success:function(data){
                console.log(data);
                var html = '';
                for(var key in data){
                    var item = data[key];
                    html += '<option value="'+item.id+'">'+item.name+'</option>';
                }
                
                $("select[name='adv_object_id']").html(html);
            }
            
        })
    })
        $("#advAdd").click(function () {
            var validator = $("#addAdvForm").data("bs.validator");
            validator.validate();
            if(!validator.hasErrors()) {
                $.ajax({
                    url: "{:U('Admin/Content/doAddAdv')}",
                    type: "post",
                    dataType: "json",
                    data: $("#addAdvForm").serialize(),
                    /*data: {
                    name: $("input[name='name']").val(),
                    prid:$("input[name='prid']").val(),
                    position:$("input[name='position']").val(),
                    introduce: $("input[name='introduce']").val(),
                    status: $("input[name='status']").val(),
                },*/
                    success: function (data) {
                    if(data){
                        $("#commonModal").modal('toggle');
                        common.alert("提示","广告轮播添加成功!",function (){
                            common.reload();
                        });
                    }
                }
            });
            }
        })
        //广告图片上传
       $("#advImg").click(function(){
           $('#fileupload').click();
       })
       $('#fileupload').fileupload({
           url: "{:U('Admin/Upload/index')}",
           dataType: "",
           formData: {type: "adver"},
           autoUpload: true,
           done: function (e, data) {
               var files = data.result;
               var file = files[0];
               $("#advImg").attr("src","__ROOT__"+file.filePath);
               $("#resource").val(file.filePath);
           }
       });
    </script>